package com.example.demo.Controller;

//         佛祖保佑       永无BUG     永不修改

import com.example.demo.Service.userService;
import com.example.demo.pojo.UserDO;
import com.example.demo.pojo.flower;
import com.example.demo.uitls.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class UserController {

    @Autowired
    private userService userService;

    /**
     * 登录
     * @param username  账号
     * @param password  密码
     * @return
     */
    @PostMapping(value = "/lgoin")
    public UserDO  login(@RequestParam(value = "username") String username,
                        @RequestParam(value = "password") String password){
        return userService.loginUser(username,password);

    }

    /**
     * 注册账号
     * @param userDO
     * @return
     */
    @PostMapping(value = "/setUser")
    public boolean setUser(UserDO userDO){
        return userService.setUser(userDO);
    }

    @PostMapping(value = "/upload")
    public String upload(MultipartFile file, flower flower) {
        String flowers = file.getOriginalFilename();
        String picture = FileUtil.renameToUUID(flowers);
        try {
            FileUtil.uploadFile(file.getBytes(), "D:\\nginx-1.18.0\\nginx-1.18.0\\html", picture);
            userService.upload(flower,"http://localhost/"+picture,flowers);
            return "http://localhost/"+picture;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


}
